
package com.sixfoot.adminsys.frames.user;

import com.sixfoot.domain.Role;
import com.sixfoot.domain.User;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;


public class UserTableModel extends AbstractTableModel {

    public static final int realName_column = 0;
    public static final int sex_column = 1;
    public static final int birthday_column = 2;
    public static final int mobile_column = 3;
    public static final int height_column = 4;
    public static final int weight_column = 5;
    public static final int workingPlace_column = 6;
    public static final int position_column = 7;
    public static final int medicalHistory_column = 8;
    public static final int locked_column = 9;
    public static final int roles_column = 10;
    public static final int COLUMN_COUNT = 11;

    private final List<User> users = new ArrayList<>();

    public void add(List<User> newUsers) {
        int first = users.size();
        int last = first + newUsers.size() - 1;
        users.addAll(newUsers);
        fireTableRowsInserted(first, last);
    }

    public void add(User user) {
        int index = users.size();
        users.add(user);
        fireTableRowsInserted(index, index);
    }

    public void update(User user, int row) {
        int index = users.size();
        users.remove(row);
        users.add(row, user);
        fireTableRowsUpdated(index-1, index-1);
    }

    public int getRowCount() {
        return users.size();
    }

    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    @Override
    public Class getColumnClass(int column) {
        if (getValueAt(0, column) == null) {
            return "".getClass();
        }
        return getValueAt(0, column).getClass();
    }

    public User getUser(int row) {
        return users.get(row);
    }

    public Object getValueAt(int row, int column) {
        User user = users.get(row);
        switch (column) {
            case realName_column:
                return user.getRealName();
            case sex_column:
                return user.getSex() == 0 ? "女" : "男";
            case birthday_column:
                return user.getBirthday();
            case mobile_column:
                return user.getMobile();
            case height_column:
                return user.getHeight();
            case weight_column:
                return user.getWeight();
            case workingPlace_column:
                return user.getWorkingPlace();
            case position_column:
                return user.getPosition();
            case medicalHistory_column:
                return user.getMedicalHistory();
            case locked_column:
                return user.getLocked() == true ? "锁定" : "未锁定";
            case roles_column:
                String roleNames = "";
                if (user.getRoles() != null && user.getRoles().size() > 0) {
                    StringBuilder roleNameBuilder = new StringBuilder();
                    for (Role r : user.getRoles()) {
                        roleNameBuilder.append(r.getName()).append(",");
                    }
                    roleNames = roleNameBuilder.substring(0, roleNameBuilder.length() - 1);
                }
                return roleNames;


        }
        return null;
    }
}